package com.mit.service.maintenance.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.mit.common.exception.NoRecordUpdateException;
import com.mit.common.maintenance.dto.EmployeeDTO;
import com.mit.common.security.dto.UserDTO;
import com.mit.service.core.dao.AbstractDAO;
import com.mit.service.maintenance.dao.IEmployeeDAO;

public class EmployeeDAOImpl extends AbstractDAO implements IEmployeeDAO {
	private static final Logger logger = Logger.getLogger(EmployeeDAOImpl.class);

	@Override
	public EmployeeDTO createEmployee(EmployeeDTO employeeDTO) {
		Integer id = (Integer) getSqlMapClientTemplate().insert("insertEmployee", employeeDTO);
		employeeDTO.setId(id);
		return employeeDTO;
	}

	@Override
	public EmployeeDTO updateEmployee(EmployeeDTO employeeDTO) throws NoRecordUpdateException {
		logger.debug("employeeDTO: " + employeeDTO);
		int rs = getSqlMapClientTemplate().update("updateEmployee", employeeDTO);
		if(rs == 0) {
			throw new NoRecordUpdateException();
		}
		return employeeDTO;
	}

	@Override
	public void deleteEmployeeById(int empId) {
		int count = getSqlMapClientTemplate().delete("deleteEmployeeById", empId);
		logger.debug("no. of record updated:" + count + " by empId=" + empId);
	}

	@Override
	public boolean isEmployeeExisted(EmployeeDTO employeeDTO) {
		Integer count = (Integer) getSqlMapClientTemplate().queryForObject("isEmployeeExisted", employeeDTO);
		if (count.intValue() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public EmployeeDTO retrieveEmployeeById(int empId) {
		return (EmployeeDTO) getSqlMapClientTemplate().queryForObject("getEmployee", empId);
	}

	@Override
	public Integer retrieveEmployeeCount(UserDTO user) {
		logger.debug("user:" + user);
		Integer count = (Integer) getSqlMapClientTemplate().queryForObject("retrieveEmployeeCount", user);		
		return count;
	}
	
	@Override
	public List<EmployeeDTO> retrieveEmployees(HashMap params) {
		logger.debug("params:" + params);
		List<EmployeeDTO> lst = (List<EmployeeDTO>) getSqlMapClientTemplate().queryForList("retrieveEmployees", params);		
		return lst;
	}
}
